package zjc.bookkeeping.service.imp;

import android.accounts.AccountAuthenticatorActivity;
import android.hardware.lights.Light;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import zjc.bookkeeping.minepage.dAccountbookmanagement.AddbookDialog;
import zjc.bookkeeping.model.Account;
import zjc.bookkeeping.model.User;
import zjc.bookkeeping.server.MyHttpUtil;
import zjc.bookkeeping.server.StaticData;
import zjc.bookkeeping.service.AccountService;

public class AccountServiceImp implements AccountService {

    @Override
    public String addAccount(Account account) {
        final String[] accountKey = {null};
        String url = MyHttpUtil.host + "/account/addAccount";
        Gson gson = new Gson();
        String json = gson.toJson(account);
        Log.i("json",json);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),json);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        OkHttpClient client=new OkHttpClient();
        Thread addThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.i("---","--------第一次————————————————");
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseData);
                    String uid = jsonObject.optString("data",null);
                    accountKey[0] = uid;
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        addThread.start();
        try {
            addThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        MyHttpUtil.sendOkhttpPostRequest(url, requestBody, new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.i("zjc",e.getMessage());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String responseData = response.body().string();
//                Log.i("responseData",responseData);
//                try {
//                    JSONObject jsonObject = new JSONObject(responseData);
//                    String uid = jsonObject.optString("data",null);
//                    accountKey[0] = uid;
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
        Log.i("---","--------第二次————————————————");
        return accountKey[0];
    }

    @Override
    public List<Account> findAllAccount(int userId) {
        List<Account> accountList = new ArrayList<>();
        String url = MyHttpUtil.host + "/account/findAllAccount";
        FormBody.Builder params = new FormBody.Builder();
        params.add("userId", String.valueOf(userId));
        Request request = new Request.Builder()
                .url(url)
                .post(params.build())
                .build();
        OkHttpClient client=new OkHttpClient();
        Thread addThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.i("---","--------第一次————————————————");
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseData);
                    String data = jsonObject.optString("data",null);
                    if (!data.isEmpty()){
                        Log.i("data",data);
                        JSONArray array = new JSONArray(data);
                        for(int i = 0; i <array.length(); i++){
                            JSONObject object = array.getJSONObject(i);
                            Log.i("object", String.valueOf(object));
                            Account account = new Gson().fromJson(String.valueOf(object),Account.class);
                            accountList.add(account);
                        }
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        addThread.start();
        try {
            addThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        MyHttpUtil.sendOkhttpPostRequest(url,params.build(), new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.i("zjc",e.getMessage());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String responseData = response.body().string();
//                Log.i("responseData",responseData);
//                try {
//                    JSONObject jsonObject = new JSONObject(responseData);
//                    String data = jsonObject.optString("data",null);
//                    Log.i(data,data);
//                    if (!data.isEmpty()){
//                        Log.i("data",data);
//                        JSONArray array = new JSONArray(data);
//                        for(int i = 0; i <array.length(); i++){
//                            JSONObject object = array.getJSONObject(i);
//                            Log.i("object", String.valueOf(object));
//                            Account account = new Gson().fromJson(String.valueOf(object),Account.class);
//                            accountList.add(account);
////                            String accountId = object.optString("accountId",null);
////                            String accountName = object.optString("accountName",null);
////                            String accountRemarks = object.optString("accountRemarks",null);
////                            String accountTime = object.optString("accountTime",null);
////                            int accountType = object.optInt("accountType",0);
////                            String accountUid = object.optString("accountUid",null);
////                            int grade = object.optInt("grade",0);
////                            int userId = object.optInt("userId",0);
////                            String userRemarks = object.optString("userRemarks",null);
//
//                        }
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
        return accountList;
    }

    @Override
    public boolean deleteAccount(String accountUid) {
        final boolean[] deleteFlag = {false};
        String url = MyHttpUtil.host + "/account/deleteAccount";
        FormBody.Builder params = new FormBody.Builder();
        params.add("accountUid",accountUid);
        Request request = new Request.Builder()
                .url(url)
                .post(params.build())
                .build();
        OkHttpClient client=new OkHttpClient();
        Thread addThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.i("---","--------第一次————————————————");
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseData);
                    String data = jsonObject.optString("data",null);
                    if (data.equals("success")){
                        deleteFlag[0] = true;
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        addThread.start();
        try {
            addThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        MyHttpUtil.sendOkhttpPostRequest(url,params.build(), new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.i("zjc",e.getMessage());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String responseData = response.body().string();
//                Log.i("responseData",responseData);
//                try {
//                    JSONObject jsonObject = new JSONObject(responseData);
//                    String data = jsonObject.optString("data",null);
//                    if (data.equals("success")){
//                        deleteFlag[0] = true;
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
        return deleteFlag[0];
    }
}
